﻿namespace AntdUI_HamburgerMenuTabs.Common
{
    internal class Common
    {
        /// <summary>
        /// 把字节长度格式化为易读的数字和单位
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public static long FormatSize(long bytes, out string unit)
        {
            string[] suffixes = ["B", "KB", "MB", "GB", "TB"];
            int order = 0;
            double size = bytes;

            while (size >= 1024 && order < suffixes.Length - 1)
            {
                order++;
                size /= 1024;
            }

            unit = suffixes[order];

            return (long)size;
        }

        /// <summary>
        /// 把字节长度格式化为易读的字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string FormatSize(long bytes)
        {
            var size = FormatSize(bytes, out string unit);
            return $"{size:0.##} {unit}";
        }
    }
}